<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;

class Statistics extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function schoolStats()
    {
        $scopeType = $this->request->get('scope_type', 'city');
        $scopeId = $this->request->get('scope_id');
        $regionMode = $this->request->get('region_mode', 'self');
        $teachMode = $this->request->get('teach_mode', 'all');
        $dateStart = $this->request->get('date_start');
        $dateEnd = $this->request->get('date_end');
        try {
            $schoolWhere = ['status' => 1];
            if ($scopeType === 'province' && $scopeId) $schoolWhere['province_id'] = $scopeId;
            if ($scopeType === 'city' && $scopeId) $schoolWhere['city_id'] = $scopeId;
            if ($scopeType === 'school' && $scopeId) $schoolWhere['id'] = $scopeId;
            $schoolCount = Db::name('eb_school')->where($schoolWhere)->count();

            $classWhere = ['status' => 1];
            if ($scopeType === 'school' && $scopeId) $classWhere['school_id'] = $scopeId;
            if ($scopeType === 'city' && $scopeId) $classWhere['city_id'] = $scopeId;
            if ($teachMode === 'offline') $classWhere['teach_mode'] = 'offline';
            if ($teachMode === 'online') $classWhere['teach_mode'] = 'online';
            if ($dateStart) $classWhere['create_time'] = ['>=', $dateStart];
            if ($dateEnd) $classWhere['update_time'] = ['<=', $dateEnd];
            $classCount = Db::name('eb_classes')->where($classWhere)->count();

            $offlinePeople = 0;
            $onlinePeople = 0;
            try {
                $offlinePeople = Db::name('eb_class_student')->where(['status' => 1, 'learn_mode' => 'offline'])->count();
                $onlinePeople = Db::name('eb_class_student')->where(['status' => 1, 'learn_mode' => 'online'])->count();
            } catch (\Exception $e) {
            }

            return json(['code' => 200, 'msg' => 'OK', 'data' => [
                'school_count' => $schoolCount,
                'class_count' => $classCount,
                'offline_people' => $offlinePeople,
                'online_people' => $onlinePeople
            ]]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function classNamesBySchool()
    {
        $schoolId = $this->request->get('school_id');
        $schoolName = $this->request->get('school_name');
        try {
            if (!$schoolId && $schoolName) {
                $sid = Db::name('eb_school')->where('name', $schoolName)->value('id');
                $schoolId = $sid ?: 0;
            }
            if (!$schoolId) {
                return json(['code' => 200, 'msg' => 'OK', 'data' => ['list' => []]]);
            }
            $rows = Db::name('eb_classes')
                ->where('school_id', $schoolId)
                ->where('status', 1)
                ->field('id,name,name as title')
                ->limit(50)
                ->select();
            $list = [];
            foreach ($rows as $r) {
                $list[] = ['id' => $r['id'], 'name' => isset($r['name']) && $r['name'] ? $r['name'] : ($r['title'] ?? ('班级'.$r['id']))];
            }
            return json(['code' => 200, 'msg' => 'OK', 'data' => ['list' => $list]]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => ['list' => []]]);
        }
    }

    public function classStats()
    {
        $scopeType = $this->request->get('scope_type', 'city');
        $scopeId = $this->request->get('scope_id');
        try {
            $where = ['status' => 1];
            if ($scopeType === 'school' && $scopeId) $where['school_id'] = $scopeId;
            if ($scopeType === 'city' && $scopeId) $where['city_id'] = $scopeId;
            $total = Db::name('eb_classes')->where($where)->count();
            $schoolClass = Db::name('eb_classes')->where($where)->where('class_scene', 'school')->count();
            $communityClass = Db::name('eb_classes')->where($where)->where('class_scene', 'community')->count();
            $outsideClass = Db::name('eb_classes')->where($where)->where('class_scene', 'outside')->count();
            return json(['code' => 200, 'msg' => 'OK', 'data' => [
                'total' => $total,
                'school' => $schoolClass,
                'community' => $communityClass,
                'outside' => $outsideClass
            ]]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function classDetail()
    {
        $scopeType = $this->request->get('scope_type', 'city');
        $scopeId = $this->request->get('scope_id');
        try {
            $where = ['status' => 1];
            if ($scopeType === 'school' && $scopeId) $where['school_id'] = $scopeId;
            if ($scopeType === 'city' && $scopeId) $where['city_id'] = $scopeId;
            $classes = Db::name('eb_classes')->where($where)->field('id,name,class_scene')->select();
            $list = [];
            foreach ($classes as $c) {
                $count = 0;
                try {
                    $count = Db::name('eb_class_student')->where('class_id', $c['id'])->where('status', 1)->count();
                } catch (\Exception $e) {
                }
                $list[] = ['id' => $c['id'], 'name' => $c['name'], 'scene' => $c['class_scene'], 'people' => $count];
            }
            return json(['code' => 200, 'msg' => 'OK', 'data' => $list]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }
}